package com.lin.eat.serivce.impl;

import com.alibaba.fastjson.JSONObject;
import com.lin.eat.dao.CustomerMapper;
import com.lin.eat.dao.GoodsCommentMapper;
import com.lin.eat.dao.GoodsMapper;
import com.lin.eat.dao.OrderMapper;
import com.lin.eat.entry.*;
import com.lin.eat.serivce.OrderService;
import com.lin.eat.util.JwtUtil;
import com.lin.eat.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    GoodsCommentMapper goodsCommentMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    CustomerMapper customerMapper;

    /**
     * 提交订单
     * @param order
     * @param token
     * @return
     */
    @Override
    public ServerResponse<JSONObject> submitOrder(Order order, String token) {
        ServerResponse<JSONObject> serverResponse = null;
        String Cid = JwtUtil.parseJwt(token).getId();
        String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        order.setO_id(UuidUtil.createUUID());
        order.setC_id(Cid);
        order.setO_time(time);
        System.out.println(order);
        int i = orderMapper.submitOrder(order);
        int sum = 0;
        for(OrderDetail orderDetail: order.getOrderDetailList()){
            orderDetail.setOd_id(UuidUtil.createUUID());
            orderDetail.setO_id(order.getO_id());
            int result = orderMapper.addOrderDetail(orderDetail);
            if (result > 0)
                sum++;
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("o_id",order.getO_id());
        serverResponse = i>0&&sum==order.getOrderDetailList().size() ? ServerResponse.createBySuccess(jsonObject) : ServerResponse.createByError(null);
        return serverResponse;
    }

    /**
     * 获取订单详情
     * @param o_id
     * @return
     */
    @Override
    public ServerResponse<Order> getOrderDetail(String o_id) {
        ServerResponse<Order> serverResponse = null;
        Order order = orderMapper.getOrderDetailByOid(o_id);
        serverResponse = order!=null ? ServerResponse.createBySuccess(order) : ServerResponse.createByError(null);
        return serverResponse;
    }

    /**
     * 支付订单
     * @param o_id
     * @return
     */
    @Override
    public ServerResponse updateOrderState(String o_id) {
        ServerResponse serverResponse = null;
        int i = orderMapper.updateOrderState(o_id);
        serverResponse = i>0 ? ServerResponse.createBySuccess(null) : ServerResponse.createByError(null);
        return serverResponse;
    }

    /**
     * 获取所有订单
     * @return
     */
    @Override
    public ServerResponse<List<Order>> getAllOrder() {
        ServerResponse<List<Order>> serverResponse = null;
        List<Order> orderList = orderMapper.getAllOrder();
        serverResponse = orderList.size()>0 ? ServerResponse.createBySuccess(orderList) : ServerResponse.createByError(null);
        return serverResponse;
    }

    /**
     * 删除订单
     * @param o_id
     * @return
     */
    @Override
    public ServerResponse deleteOrder(String o_id) {
        ServerResponse<List<Order>> serverResponse = null;
        int i = orderMapper.deleteOrder(o_id);
        serverResponse = i>0 ? ServerResponse.createBySuccess(null) : ServerResponse.createByError(null);
        return serverResponse;
    }

    /**
     * 提交评论
     * @param orderComment
     * @return
     */
    @Override
    public ServerResponse submitOrderComment(OrderComment orderComment) {
        ServerResponse serverResponse = null;
        Customer customer = customerMapper.findCustomerByCid(orderComment.getC_id());
        String bid = goodsMapper.findBidByGid(orderComment.getG_id());
        String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        GoodsComment goodsComment = new GoodsComment(UuidUtil.createUUID(), orderComment.getC_id(), bid, orderComment.getG_id(), customer.getC_image(), customer.getC_name(), orderComment.getGc_comment(), time);
        int i = goodsCommentMapper.addComment(goodsComment);
        int result = orderMapper.updateOrderDetailState(orderComment.getOd_id());
        serverResponse = i>0&&result>0 ? ServerResponse.createBySuccess(null) : ServerResponse.createByError(null);
        return serverResponse;
    }

}
